package com.lanou3g.bookstore.book.web.servlet.admin;

import com.lanou3g.bookstore.book.domain.Book;
import com.lanou3g.bookstore.book.service.BookService;
import com.lanou3g.bookstore.category.domain.Category;
import com.lanou3g.bookstore.category.service.CategoryService;
import com.lanou3g.bookstore.util.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/adminjsps/admin/adminBook.do")
public class AdminBookServlet extends BaseServlet {
    private BookService bookService = new BookService();
    private CategoryService categoryService = new CategoryService();

    private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Book> books = bookService.findAll();
        request.setAttribute("books", books);
        request.getRequestDispatcher("/adminjsps/admin/book/list.jsp").forward(request, response);
    }

    private void load(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bid = request.getParameter("bid");
        Book book = bookService.load(bid);
        request.setAttribute("book", book);
        List<Category> bookClass = categoryService.findAll();
        request.setAttribute("bookClass", bookClass);
        request.getRequestDispatcher("/adminjsps/admin/book/desc.jsp").forward(request, response);
    }

    private void addPre(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> categoryList = categoryService.findAll();
        request.setAttribute("categoryList", categoryList);
        request.getRequestDispatcher("/adminjsps/admin/book/add.jsp")
                .forward(request, response);
    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bid = request.getParameter("bid");
        bookService.del(bid);
        List<Book> books = bookService.findAll();
        request.setAttribute("books", books);
        request.getRequestDispatcher("/adminjsps/admin/book/list.jsp").forward(request, response);
    }

    private void mod(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = new Book();
        String bid = request.getParameter("bid");
        String bname = request.getParameter("bname");
        String price = request.getParameter("price");
        String author = request.getParameter("author");
        String image = request.getParameter("image");
        String cid = request.getParameter("cid");
        book.setBid(bid)
                .setBname(bname)
                .setPrice(Double.valueOf(price))
                .setAuthor(author)
                .setImage(image)
                .setCid(cid);
        bookService.mod(book);
        List<Book> books = bookService.findAll();
        request.setAttribute("books", books);
        request.getRequestDispatcher("/adminjsps/admin/book/list.jsp")
                .forward(request, response);
    }
}
